package com.example.myapplication.ucrop;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.fragment.app.Fragment;

import com.luck.picture.lib.PictureCustomCameraActivity;
import com.luck.picture.lib.PictureSelectorActivity;
import com.luck.picture.lib.PictureSelectorCameraEmptyActivity;
import com.luck.picture.lib.PictureSelectorWeChatStyleActivity;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.config.PictureSelectionConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import com.luck.picture.lib.manager.UCropManager;
import com.luck.picture.lib.style.PictureWindowAnimationStyle;
import com.luck.picture.lib.tools.DateUtils;
import com.luck.picture.lib.tools.DoubleUtils;
import com.luck.picture.lib.tools.PictureFileUtils;
import com.luck.picture.lib.tools.SdkVersionUtils;
import com.luck.picture.lib.tools.ToastUtils;
import com.yalantis.ucrop.UCrop;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: wangrongfeng
 * @create: 2023-04-25 13:58
 **/
public class PictureCropSelectorActivity extends PictureSelectorActivity {
    @Override
    public void onPictureClick(LocalMedia media, int position) {
        if (config.selectionMode == PictureConfig.SINGLE && config.isSingleDirectReturn) {
            List<LocalMedia> list = new ArrayList<>();
            list.add(media);
            if (config.enableCrop && PictureMimeType.isHasImage(media.getMimeType()) && !config.isCheckOriginalImage) {
                mAdapter.bindSelectData(list);
                ofCrop(this, media.getPath(), media.getMimeType());
            } else {
                handlerResult(list);
            }
        } else {
            List<LocalMedia> data = mAdapter.getData();
            startPreview(data, position);
        }
    }

    public void ofCrop(Activity activity, String originalPath, String mimeType) {
        if (DoubleUtils.isFastDoubleClick()) {
            return;
        }
        if (TextUtils.isEmpty(originalPath)) {
            ToastUtils.s(activity.getApplicationContext(), activity.getString(com.luck.picture.lib.R.string.picture_not_crop_data));
            return;
        }
        PictureSelectionConfig config = PictureSelectionConfig.getInstance();
        boolean isHttp = PictureMimeType.isHasHttp(originalPath);
        String suffix = mimeType.replace("image/", ".");
        File file = new File(PictureFileUtils.getDiskCacheDir(activity.getApplicationContext()),
                TextUtils.isEmpty(config.renameCropFileName) ? DateUtils.getCreateFileName("IMG_CROP_") + suffix : config.renameCropFileName);
        Uri uri = isHttp || SdkVersionUtils.checkedAndroid_Q() ? Uri.parse(originalPath) : Uri.fromFile(new File(originalPath));
        UCrop.Options options = UCropManager.basicOptions(activity);

        Intent mCropIntent = new Intent();
        Bundle mCropOptionsBundle = new Bundle();
        mCropOptionsBundle.putParcelable(UCrop.EXTRA_INPUT_URI, uri);
        mCropOptionsBundle.putParcelable(UCrop.EXTRA_OUTPUT_URI, Uri.fromFile(file));
        mCropOptionsBundle.putAll(options.getOptionBundle());

        mCropIntent.setClass(activity, UCropCustomActivity.class);
        mCropIntent.putExtras(mCropOptionsBundle);

        activity.startActivityForResult(mCropIntent, UCrop.REQUEST_CROP);
        activity.overridePendingTransition(PictureSelectionConfig.windowAnimationStyle.activityCropEnterAnimation, com.yalantis.ucrop.R.anim.ucrop_anim_fade_in);
    }


    public static void forResult(OnResultCallbackListener<LocalMedia> listener, Activity activity) {
        if (!DoubleUtils.isFastDoubleClick()) {
            // 绑定回调监听
            PictureSelectionConfig.listener = new WeakReference<>(listener).get();
            PictureSelectionConfig.getInstance().isCallbackMode = true;
            Intent intent  = new Intent(activity, PictureCropSelectorActivity.class);
            activity.startActivity(intent);
            PictureWindowAnimationStyle windowAnimationStyle = PictureSelectionConfig.windowAnimationStyle;
            activity.overridePendingTransition(
                    windowAnimationStyle.activityEnterAnimation, com.luck.picture.lib.R.anim.picture_anim_fade_in);
        }
    }
}
